package com.demo.dao.mapper;

import com.demo.model.User2;

import static org.apache.ibatis.jdbc.SqlBuilder.*;

public class User2Mapper {
    public String insert(User2 user2) {
        return "insert into user2 (phone,username) values (#{phone},#{username})";
    }

    public String delete(int id) {
        BEGIN();
        DELETE_FROM("user2");
        WHERE("id=#{id}");
        return SQL();
    }

    public String update(User2 user2) {
        BEGIN();
        UPDATE("user2");
        if (user2.getPhone() != null && user2.getPhone().length() > 0) {
            SET("phone=#{phone}");
        }
        if (user2.getUsername() != null && user2.getUsername().length() > 0) {
            SET("username=#{username}");
        }
        WHERE("id=#{id}");
        String sql = SQL();
        System.out.println(sql);
        return sql;
    }

    public String select(String username) {
        if (username == null || username.length() == 0) {
            return "select * from user2 order by id desc";
        } else {
            return "select * from user2 where username=#{username} order by id desc";
        }
    }

    public String selectOne(Integer id) {
        return "select * from user2 where id=#{id}";
    }
}
